home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ohlutil / filemode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  5.4 KB  |  222 lines

  1. /* filemode.c -- make a string describing file modes
  2.    Copyright (C) 1985, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /*
  19.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  20.  *
  21.  * To this port, the same copying conditions apply as to the
  22.  * original release.
  23.  *
  24.  * IMPORTANT:
  25.  * This file is not identical to the original GNU release!
  26.  * You should have received this code as patch to the official
  27.  * GNU release.
  28.  *
  29.  * MORE IMPORTANT:
  30.  * This port comes with ABSOLUTELY NO WARRANTY.
  31.  *
  32.  * $Header: e:/gnu/fileutil/RCS/filemode.c'v 1.3.0.2 90/06/29 00:46:43 tho Stable $
  33.  */
  34.  
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #ifdef _POSIX_SOURCE
  38. #define S_IREAD S_IRUSR
  39. #define S_IWRITE S_IWUSR
  40. #define S_IEXEC S_IXUSR
  41. #endif
  42.  
  43. #ifdef MSDOS
  44. extern void filemodestring (struct stat *, char *);
  45. extern void mode_string (unsigned short mode, char *str);
  46. static char ftypelet (unsigned short bits);
  47. static void rwx (unsigned short, char *);
  48. static void setst (unsigned short, char *);
  49. #endif /* MSDOS */
  50.  
  51. void mode_string ();
  52. static char ftypelet ();
  53. static void rwx ();
  54. static void setst ();
  55.  
  56. /* filemodestring - fill in string STR with an ls-style ASCII
  57.    representation of the st_mode field of file stats block STATP.
  58.    10 characters are stored in STR; no terminating null is added.
  59.    The characters stored in STR are:
  60.  
  61.    0    File type.  'd' for directory, 'c' for character
  62.     special, 'b' for block special, 'm' for multiplex,
  63.     'l' for symbolic link, 's' for socket, 'p' for fifo,
  64.     '-' for any other file type
  65.  
  66.    1    'r' if the owner may read, '-' otherwise.
  67.  
  68.    2    'w' if the owner may write, '-' otherwise.
  69.  
  70.    3    'x' if the owner may execute, 's' if the file is
  71.     set-user-id, '-' otherwise.
  72.     'S' if the file is set-user-id, but the execute
  73.     bit isn't set.
  74.  
  75.    4    'r' if group members may read, '-' otherwise.
  76.  
  77.    5    'w' if group members may write, '-' otherwise.
  78.  
  79.    6    'x' if group members may execute, 's' if the file is
  80.     set-group-id, '-' otherwise.
  81.     'S' if it is set-group-id but not executable.
  82.  
  83.    7    'r' if any user may read, '-' otherwise.
  84.  
  85.    8    'w' if any user may write, '-' otherwise.
  86.  
  87.    9    'x' if any user may execute, 't' if the file is "sticky"
  88.     (will be retained in swap space after execution), '-'
  89.     otherwise.
  90.     'T' if the file is sticky but not executable. */
  91.  
  92. void
  93. filemodestring (statp, str)
  94.      struct stat *statp;
  95.      char *str;
  96. {
  97.   mode_string (statp->st_mode, str);
  98. }
  99.  
  100. /* Like filemodestring, but only the relevant part of the `struct stat'
  101.    is given as an argument. */
  102.  
  103. void
  104. mode_string (mode, str)
  105.      unsigned short mode;
  106.      char *str;
  107. {
  108.   str[0] = ftypelet (mode);
  109.   rwx ((mode & 0700) << 0, &str[1]);
  110.   rwx ((mode & 0070) << 3, &str[4]);
  111.   rwx ((mode & 0007) << 6, &str[7]);
  112.   setst (mode, str);
  113. }
  114.  
  115. /* Return a character indicating the type of file described by
  116.    file mode BITS:
  117.    'd' for directories
  118.    'b' for block special files
  119.    'c' for character special files
  120.    'm' for multiplexor files
  121.    'l' for symbolic links
  122.    's' for sockets
  123.    'p' for fifos
  124.    '-' for any other file type. */
  125.  
  126. static char
  127. ftypelet (bits)
  128.      unsigned short bits;
  129. {
  130.   switch (bits & S_IFMT)
  131.     {
  132.     default:
  133.       return '-';
  134.     case S_IFDIR:
  135.       return 'd';
  136. #ifdef S_IFLNK
  137.     case S_IFLNK:
  138.       return 'l';
  139. #endif
  140. #ifdef S_IFCHR
  141.     case S_IFCHR:
  142.       return 'c';
  143. #endif
  144. #ifdef S_IFBLK
  145.     case S_IFBLK:
  146.       return 'b';
  147. #endif
  148. #ifdef S_IFMPC
  149.     case S_IFMPC:
  150.     case S_IFMPB:
  151.       return 'm';
  152. #endif
  153. #ifdef S_IFSOCK
  154.     case S_IFSOCK:
  155.       return 's';
  156. #endif
  157. #ifdef S_IFIFO
  158. #if S_IFIFO != S_IFSOCK
  159.     case S_IFIFO:
  160.       return 'p';
  161. #endif
  162. #endif
  163. #ifdef S_IFNWK            /* HP-UX */
  164.     case S_IFNWK:
  165.       return 'n';
  166. #endif
  167.     }
  168. }
  169.  
  170. /* Look at read, write, and execute bits in BITS and set
  171.    flags in CHARS accordingly. */
  172.  
  173. static void
  174. rwx (bits, chars)
  175.      unsigned short bits;
  176.      char *chars;
  177. {
  178.   chars[0] = (bits & S_IREAD) ? 'r' : '-';
  179.   chars[1] = (bits & S_IWRITE) ? 'w' : '-';
  180.   chars[2] = (bits & S_IEXEC) ? 'x' : '-';
  181. }
  182.  
  183. /* Set the 's' and 't' flags in file attributes string CHARS,
  184.    according to the file mode BITS. */
  185.  
  186. static void
  187. setst (bits, chars)
  188.      unsigned short bits;
  189.      char *chars;
  190. {
  191. #ifdef S_ISUID
  192.   if (bits & S_ISUID)
  193.     {
  194.       if (chars[3] != 'x')
  195.     /* Set-uid, but not executable by owner. */
  196.     chars[3] = 'S';
  197.       else
  198.     chars[3] = 's';
  199.     }
  200. #endif
  201. #ifdef S_ISGID
  202.   if (bits & S_ISGID)
  203.     {
  204.       if (chars[6] != 'x')
  205.     /* Set-gid, but not executable by group. */
  206.     chars[6] = 'S';
  207.       else
  208.     chars[6] = 's';
  209.     }
  210. #endif
  211. #ifdef S_ISVTX
  212.   if (bits & S_ISVTX)
  213.     {
  214.       if (chars[9] != 'x')
  215.     /* Sticky, but not executable by others. */
  216.     chars[9] = 'T';
  217.       else
  218.     chars[9] = 't';
  219.     }
  220. #endif
  221. }
  222.